package com.project.myxima.presenters;

import com.project.myxima.data.XimalayaApi;
import com.project.myxima.interfaces.IRecommendPresenter;
import com.project.myxima.interfaces.IRecommendViewCallback;
import com.project.myxima.utils.LogUtils;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.album.GussLikeAlbumList;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class RecommendPresenter implements IRecommendPresenter {
    private static final String TAG = "RecommendPresenter";
    private List<IRecommendViewCallback> mCallbacks = new ArrayList<>();
    private Stack<Integer> re = new Stack<>();
    private List<Album> mCurrentRecommend = null ;

    private RecommendPresenter(){}

    private static RecommendPresenter sInstance = null;


    /**
     * 获取单例对象
     * @return sInstance
     */
    public static RecommendPresenter getInstance(){
        if (sInstance==null) {
            synchronized (RecommendPresenter.class){
                if(sInstance==null){
                    sInstance = new RecommendPresenter();
                }
            }
        }
        return sInstance;
    }


    /**
     * get the current recommend list
     * @return judge null before use
     */
    public List<Album> getCurrentRecommend(){
        return mCurrentRecommend;
    }

    @Override
    public void getRecommendList() {
        updateLoading();

        XimalayaApi ximalayaApi = XimalayaApi.getXimalayaApi();
        ximalayaApi.getRecommendList(new IDataCallBack<GussLikeAlbumList>() {
            @Override
            public void onSuccess(GussLikeAlbumList gussLikeAlbumList) {
                LogUtils.d(TAG,"thread name --->" + Thread.currentThread().getName());
                if(gussLikeAlbumList != null){
                    List<Album> mAlbumList = gussLikeAlbumList.getAlbumList();
                    handleRecommendResult(mAlbumList);
                }
            }

            @Override
            public void onError(int i, String s) {
                LogUtils.d(TAG,"error code ---> " + i);
                LogUtils.d(TAG,"error message ---> " + s);
                handleError();
            }
        });
    }

    private void handleError() {
        if (mCallbacks != null) {
            for (IRecommendViewCallback callback : mCallbacks) {
                callback.onNetworkError();
            }
        }
    }

    private void handleRecommendResult(List<Album> mAlbumList) {
        //通知UI更新

        if (mAlbumList != null) {
            //mAlbumList.clear();//测试一下，清空数据
            if(mAlbumList.size() == 0){
                for(IRecommendViewCallback callback: mCallbacks){
                    callback.onEmpty();
                }
            }else{
                for (IRecommendViewCallback callback : mCallbacks) {
                    callback.onRecommendListLoaded(mAlbumList);
                }
                this.mCurrentRecommend = mAlbumList;
            }
        }
    }

    private void updateLoading(){
        for (IRecommendViewCallback callback : mCallbacks) {
            callback.onLoading();
        }
    }

    @Override
    public void poll2RefreshMore() {

    }

    @Override
    public void loadMore() {

    }

    @Override
    public void registerViewCallback(IRecommendViewCallback callback) {
        if (mCallbacks != null &&  !mCallbacks.contains(callback)) {
            mCallbacks.add(callback);
        }
    }

    @Override
    public void unregisterViewCallback(IRecommendViewCallback callback) {
        if(mCallbacks != null){
            mCallbacks.remove(callback);
        }
    }

}
